package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.menggudf.entity.Parts;

@Repository
public class PartsDao{
    
    private static final Log log = LogFactory.getLog(PartsDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(Parts transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(Parts persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public Parts merge(Parts detachedInstance) {
        try {
            Parts result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public Parts findById(Integer id) {
        try {
            Parts instance = entityManager.find(Parts.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<Parts> getByID(Integer id) {
        // TODO Auto-generated method stub
        return null;
    }

    public Parts findByPart(String onePart, String twoPart) {
        try {
            StringBuffer sqld = new StringBuffer("from Parts p where p.onePart=?1 and p.twoPart=?2 ");
            Query query = entityManager.createQuery(sqld.toString()).setParameter(1, onePart).setParameter(2, twoPart);
            List<Parts> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("findByPart fail",e);
            throw e;
        }
    }

    public List<Parts> getList() {
        try {
            StringBuffer sqld = new StringBuffer("from Parts p ");
            sqld.append(" order by p.oneNum,p.twoNum desc ");
            Query query = entityManager.createQuery(sqld.toString());
            return query.getResultList();
        } catch (Exception e) {
            log.error("getList fail",e);
            throw e;
        }
    }

    public long getListCount() {
        try {
            StringBuffer sqld = new StringBuffer("select count(*) from Parts p ");
            Query query = entityManager.createQuery(sqld.toString());
            return (long) query.getSingleResult();
        } catch (Exception e) {
            log.error("getListCount fail",e);
            throw e;
        }
    }

    public List<Parts> getList(String onePart, String twoPart, int pageSize, int offset) {
        try {
            StringBuffer sqld = new StringBuffer("from Parts p ");
            int loop = 1;
            if(StringUtils.hasText(onePart)){
                if(loop==1){
                    sqld.append(" where p.onePart=?"+loop++);
                }else{
                    sqld.append(" p.onePart=?"+loop++);
                }
            }
            if(StringUtils.hasText(twoPart)){
                if(loop==1){
                    sqld.append(" where p.twoPart=?"+loop++);
                }else{
                    sqld.append(" p.twoPart=?"+loop++);
                }
            }
            sqld.append(" order by p.onePart,p.twoNum,p.createTime desc ");
            Query query = entityManager.createQuery(sqld.toString())
                    .setFirstResult(offset).setMaxResults(pageSize);
            loop = 1;
            if(StringUtils.hasText(onePart)){
                query.setParameter(loop++, onePart);
            }
            if(StringUtils.hasText(twoPart)){
                query.setParameter(loop++, twoPart);
            }
            return query.getResultList();
        } catch (Exception e) {
            log.error("getList fail",e);
            throw e;
        }
    }

    public long getListCount(String onePart, String twoPart) {
        try {
            StringBuffer sqld = new StringBuffer("select count(*) from Parts p ");
            int loop = 1;
            if(StringUtils.hasText(onePart)){
                if(loop==1){
                    sqld.append(" where p.onePart=?"+loop++);
                }else{
                    sqld.append(" p.onePart=?"+loop++);
                }
            }
            if(StringUtils.hasText(twoPart)){
                if(loop==1){
                    sqld.append(" where p.twoPart=?"+loop++);
                }else{
                    sqld.append(" p.twoPart=?"+loop++);
                }
            }
            sqld.append(" order by p.createTime desc ");
            Query query = entityManager.createQuery(sqld.toString());
            loop = 1;
            if(StringUtils.hasText(onePart)){
                query.setParameter(loop++, onePart);
            }
            if(StringUtils.hasText(twoPart)){
                query.setParameter(loop++, twoPart);
            }
            return (long) query.getSingleResult();
        } catch (Exception e) {
            log.error("getListCount fail",e);
            throw e;
        }
    }

}
